package com.starblink.android.basic.bridge

import com.blankj.utilcode.util.ProcessUtils
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.starblink.android.basic.ext.toJson
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.android.basic.util.bus.FlowConst
import com.starblink.android.basic.util.bus.bean.CollectProductEvent
import com.starblink.android.basic.util.bus.bean.FollowStoreEvent
import com.starblink.android.basic.util.bus.bean.RemoveStoresEvent
import com.starblink.basic.log.SKLogger
import com.starblink.web.IBridgeInvokeWebProcess
import kotlinx.coroutines.MainScope

//通知中转站
class FlowBusBridge: IBridgeCommand {
    private val TAG="IBridgeCommand"
    override fun exec(params: JsonObject?, callback: IBridgeInvokeWebProcess?) {
        SKLogger.d(TAG,"${ProcessUtils.getCurrentProcessName()} --- $params")
        params?.let {
            val flowBusEvent=it.get("flowBusEvent").asString
            val params=(it.get("params")?:"").toJson()
            val gson=Gson()
            when(flowBusEvent){
                FlowConst.FOLLOW_STORE_EVENT->{
                    val followStoreEvent= gson.fromJson(params,FollowStoreEvent::class.java)
                    //刷新首页和订阅数据
                    FlowBus.with<FollowStoreEvent>(FlowConst.FOLLOW_STORE_EVENT).post(MainScope(), followStoreEvent)
                }
                FlowConst.COLLECT_PRODUCT_EVENT->{
                    val collectProductEvent= gson.fromJson(params,CollectProductEvent::class.java)
                    //刷新app收藏状态
                    FlowBus.with<CollectProductEvent>(FlowConst.COLLECT_PRODUCT_EVENT)
                        .post(MainScope(),collectProductEvent)
                }
                FlowConst.REMOVE_STORE_EVENT->{
                    //批量取消订阅
                    val removeStoresEvent= gson.fromJson(params,RemoveStoresEvent::class.java)
                    FlowBus.with<RemoveStoresEvent>(FlowConst.REMOVE_STORE_EVENT)
                        .post(MainScope(),removeStoresEvent)
                }
                FlowConst.LOGIN_STATUS_CHANGE->{
                    //刷新app登录状态
                    FlowBus.boolFlowBusNotify(FlowConst.LOGIN_STATUS_CHANGE)
                }
                FlowConst.OTHER_ADD_PRODUCT_VIEW->{
                    //刷新app收藏的页面
                    FlowBus.boolFlowBusNotify(FlowConst.OTHER_ADD_PRODUCT_VIEW)
                }
            }
        }
    }
}